<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表页面-MyBlog</title>
    <style>
        .content{
            height: 28px;
        }
    </style>
    <script src="https://cdn.bootcss.com/jquery/2.1.2/jquery.min.js"></script>
    <script>

        $(function(){

            $("a.del").click(function(){

                var delId = $(this).attr("data-id");

                if(confirm("提示：确认删除？")) {

                    $.post('public/ajax/del_info.php', {delId:delId}, function (data) {
                        if (data == 'ok') {
                            window.location.href = "list.php";
                            return;
                        } else {
                            alert('提示：删除有误!');
                            return;
                        }
                    });

                }


            });

        });

    </script>
    <meta name="keywords" content="个人博客" />
    <meta name="description" content="" />
    <link rel="stylesheet" href="css/index.css"/>
    <link rel="stylesheet" href="css/style.css"/>
    <script type="text/javascript" src="js/jquery1.42.min.js"></script>
    <script type="text/javascript" src="js/jquery.SuperSlide.2.1.1.js"></script>
</head>
<body>

<!--header end-->
<!--nav-->
<div id="nav">
    <ul>
        <li><a href="list.php">文章列表</a></li>
        <li><a href="add_blog.php">发布文章</a></li>
        <li><a href="changePwd.php">修改密码</a></li>
        <div class="clear"></div>
    </ul>
</div>
<!--nav end-->
<!--content start-->
<div id="content">
    <!--left-->
    <div class="left" id="guestbook">
        <div class="weizi">
            <div class="wz_text">当前位置：<a href="#">首页</a>><h1>文章列表</h1></div>
        </div>
        <div class="g_content">
            <?php

            //连接数据库
            include 'inc/conn.php';

            //验证用户登陆
            include 'inc/checkSesion.php';



            $pageSize = 2; //每页显示条数

            //总条数
            $sqlCount = "SELECT COUNT(id) FROM blog_info WHERE id IS NOT NULL";
            $resCount = mysqli_query($conn, $sqlCount);
            if ($rwtCount = mysqli_fetch_array($resCount)) {
                $infoCount = $rwtCount[0]; //总条数
            }

            $pageCount = ceil($infoCount/$pageSize); //总页数

            $page = intval($_GET['page']); //当前页数
            if ($page > $pageCount) {
                $page = $pageCount;
            } elseif ($page < 1) {
                $page = 1;
            }

            $limit = ($page - 1) * $pageSize;


            //读取文章内容
            $sql = "SELECT id, blog_title, add_time FROM blog_info WHERE id IS NOT NULL ORDER BY id DESC LIMIT $limit, $pageSize  ";
            $res = mysqli_query($conn, $sql);
            while ($rwt = mysqli_fetch_array($res)) {
                $list[] = $rwt;
            }
            //var_dump($list);
            ?>




            <table width="99%" align="center" border="1">
                <tr style="height: 30px">
                    <th width="10%">ID</th>
                    <th width="50%">文章标题</th>
                    <th width="20%">发布时间</th>
                    <th width="20%">操作</th>
                </tr>

                <?php

                foreach ($list as $val) {
                    echo "<tr>
               <td class='content'>".$val['id']."</td>
               <td>".$val['blog_title']."</td>
               <td>".date('Y-m-d H:i:s', $val['add_time'])."</td>
               <td>
                    <a href='edit_blog.php?id=".$val['id']."'>编辑</a>
                    <a href='javascript:void(0);' data-id='".$val['id']."' class='del'>删除</a>
               </td>
          </tr>";
                }

                ?>


                <tr>
                    <td colspan="4">

                        总计：<?=$infoCount?>条，<?=$page.'/'.$pageCount?>

                        <a href="?page=1">首页</a>
                        <?php

                        if ($page > 1) {
                            $prevPage = $page - 1;
                            echo "<a href='?page=$prevPage'>上一页</a>";
                        }

                        if ($page < $pageCount) {
                            $nextPage = $page + 1;
                            echo "<a href='?page=$nextPage'>下一页</a>";
                        }
                        ?>


                        <a href="?page=<?=$pageCount?>">尾页</a>
                    </td>
                </tr>


            </table>
        </div>
    </div>
    <!--end left -->


<!--footer end-->
<script type="text/javascript">jQuery(".lanmubox").slide({easing:"easeOutBounce",delayTime:400});</script>
<script  type="text/javascript" src="js/nav.js"></script>
</body>

</html>